<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/5
     * Time: 下午6:32
     */

    namespace app\store\model;

    use app\payment\logic\PayBase;
    use think\Db;
    use think\Model;

    class OrderModel extends Model
    {
        const PAY_VIEW_ALIPAY = 1;
        const PAY_VIEW_WECHATPAY = 2;
        const PAY_VIEW_WALLET = 3;
        const PAY_VIEW_PRODUCT = 4;


        protected $name = 'store_order';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i', 'update_time' => 'timestamp:Y-m-d H:i'];

        const STATUS_CANCEL = 0;
        const STATUS_NO_PAY = 10;
        const STATUS_YES_PAY = 20;
        const STATUS_USER_REFUND = 21;
        const STATUS_YES_REFUND = 22;
        const STATUS_NO_REFUND = 23;
        const STATUS_DELIVER = 30;
        const STATUS_DELIVER_SIGN = 40;
        const STATUS_DELIVER_REFUND = 41;
        const STATUS_DELIVER_REFUND_YES = 42;
        const STATUS_DELIVER_REFUND_NO = 43;
        const STATUS_DELETE = 90;


        const   STATUS_VIEW = [
            self::STATUS_CANCEL => '已取消',
            self::STATUS_NO_PAY => '未付款',
            self::STATUS_YES_PAY => '已付款',
            self::STATUS_USER_REFUND => '退款审核',
            self::STATUS_YES_REFUND => '退款成功',
            self::STATUS_NO_REFUND => '退款失败',
            self::STATUS_DELIVER => '已发货',
            self::STATUS_DELIVER_SIGN => '已收货',
            self::STATUS_DELIVER_REFUND => '退货',
            self::STATUS_DELIVER_REFUND_YES => '退货成功',
            self::STATUS_DELIVER_REFUND_NO => '退货失败',
            self::STATUS_DELETE => '删除'
        ];

        /**
         * 获取运行参数
         * @param $order_sn
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function getRunData($order_sn)
        {
            $getRunData = self::where(['order_sn' => $order_sn])->find();
            return json_decode($getRunData['rundata'], true);
        }

        /**
         * 设置运行参数
         * @param $order_sn
         * @param array $data
         * @return false|int
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function setRunData($order_sn, $data = [])
        {
            $getRunData = self::where(['order_sn' => $order_sn])->find();
            $rundata = json_decode($getRunData->rundata, true);
            foreach ($data as $key => $datum) {
                $rundata[$key] = $datum;
            }

            $getRunData->rundata = json_encode($rundata);
            return $getRunData->save();
        }


        static public function getOrderBayInfo($pay_sn)
        {

            return self::where(['pay_sn' => $pay_sn])->where('order_status < 90')->select();
        }

        /**
         * 订单ID获取订单信息
         *
         * @param $order_sn
         *
         * @return array|false|\PDOStatement|string|\think\Model
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function getOrderOrderSnBuInfo($order_sn)
        {

            return self::where(['order_sn' => $order_sn])->where('order_status < 90')->find();
        }

        /**
         * 订单ID获取已经支付订单信息
         *
         * @param $order_sn
         *
         * @return array|false|\PDOStatement|string|\think\Model
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function geIsBayOrderSnBuInfo($order_sn)
        {

            return self::where(['order_sn' => $order_sn])->where(' order_status > 19 and order_status < 90')->find();
        }

        /**
         * 获取支付总额
         *
         * @param $pay_sn
         *
         * @return float|int
         */
        static public function getOrderPaySumMoneyByPaySn($pay_sn)
        {

            return self::where(['pay_sn' => $pay_sn])->sum('goods_amount');
        }

        /**
         * 取消订单
         *
         * @param $pay_sn
         *
         * @return $this
         */
        static public function cancelOrder($order_sn)
        {

            $order_sn = strval($order_sn);//strval
            $res = self::where("order_sn = '{$order_sn}

    '")->update(['order_status' => 0]);
            return $res ? true : false;
        }

        /**
         * 删除订单
         *
         * @param $pay_sn
         *
         * @return mixed
         */
        static public function delOrder($order_sn)
        {

            $order_sn = (int)$order_sn;

            $res = self::where("order_sn = '{
        $order_sn}'")->update(['order_status' => 90]);

            return $res ? true : false;
        }

        /**
         * 发货
         *
         * @param $order_sn
         *
         * @return bool
         */
        static public function deliverOrder($order_sn)
        {

            $order_sn = (int)$order_sn;

            $res = self::where("order_sn = '{
        $order_sn}'")->update(['order_status' => 30]);

            return $res ? true : false;
        }

        /**
         * 退货
         *
         * @param $order_sn
         *
         * @return bool
         */
        static public function kickbackOrder($info)
        {

            $order_sn = (int)$info['order_sn'];
            $logistics_code = $info['logistics_code'];
            $reason = $info['reason'];
            if ($logistics_code == 0 || $logistics_code == '') {
                $type = 0;
            } else {
                $type = 1;
            }

            RefundLogisticsModel::create([
                'order_sn' => $order_sn,
                'code' => $logistics_code,
                'reason' => $reason,
                'type' => $type,
                'status' => 0,
            ]);


            $res = self::where("order_sn = '{
        $order_sn}'")->update(['order_status' => 41]);


            return $res ? true : false;
        }

        /**
         * 确认收货
         *
         * @param $order_sn
         *
         * @return bool
         * @throws \think\Exception
         */
        static public function confirmOrder($order_sn)
        {

            $order_sn = (int)$order_sn;

            $res = self::where("order_sn = '{
        $order_sn}'")->update(['order_status' => 40]);

            if ($res) {//更新销量
                static::upsalenum($order_sn);
            }
            return $res ? true : false;
        }

        /**
         * 更新销量
         *
         * @param $order_sn
         *
         * @throws \think\Exception
         */
        static public function upsalenum($order_sn)
        {

            $store_salenums = Db::name('store_order_info')->where('order_sn', $order_sn)->column('goods_id,goods_num');
            foreach ($store_salenums as $key => $store_salenum) {
                Db::name('store')->where('goods_id', $key)->setInc('store_salenum', $store_salenum);
            }

        }
    }